package com.magenta.maxoptra.geocoding.ptv.util;

import com.ptvag.jabba.core.exception.SystemException;
import com.ptvag.jabba.service.baseservices.CallerContext;
import com.ptvag.jabba.service.baseservices.ClientFactory;
import com.ptvag.jabba.service.baseservices.RemoteType;
import com.ptvag.xserver.common.Point;
import com.ptvag.xserver.common.XServiceException;
import com.ptvag.xserver.xroute.*;

/**
 * this utility class contains static methods for xroute convenience
 * 
 * @author RN
 */
public class XRouteUtils {

	private XRouteUtils() {
		// this is a utility class!
	}

	public static XRouteRemoteInterface makeClient(String url) {
		XRouteRemoteInterface client = (XRouteRemoteInterface) ClientFactory
				.createClient(XRouteRemoteInterface.class,
                        RemoteType.DOCSTYLE_CXF, "", "", url);
		CallerContext cc = CommonUtils.makeDefaultContext();
		client.setCallerContext(cc);
		client.setUsername("user");
		client.setPassword("pwd");
		return client;
	}

	public static WaypointDesc makeWayPointDesc(Point pt, LinkType linkType) {
		WaypointDesc res = new WaypointDesc(new Point[] { pt }, linkType, null,
				null, 0, null, null, null, null, null, null);
		return res;
	}

	public static Route calcRoute(XRouteRemoteInterface client, Point[] points,
			DetailLevel detailLevel) throws SystemException, XServiceException,
			XRouteException {
		WaypointDesc[] waypoints = new WaypointDesc[points.length];
		for (int i = 0; i < waypoints.length; i++) {
			waypoints[i] = makeWayPointDesc(points[i], LinkType.NEXT_SEGMENT);
		}
		return calcRoute(client, waypoints, detailLevel);
	}

	public static Route calcRoute(XRouteRemoteInterface client,
			WaypointDesc[] wpts, DetailLevel detailLevel)
			throws SystemException, XServiceException, XRouteException {
		ResultListOptions rlos = new ResultListOptions();
		rlos.setDetailLevel(detailLevel);
		rlos.setPolygon(true);
		rlos.setManoeuvres(true);
		rlos.setSegments(true);
		rlos.setBinaryPathDesc(true);
		rlos.setTexts(true);
		rlos
				.setStartDetail(new DetailDescriptionOption(0,
						DetailLevel.STANDARD));
		rlos
				.setDestDetail(new DetailDescriptionOption(0,
						DetailLevel.STANDARD));
		return client.calculateRoute(wpts, new RoutingOption[0],
				new ExceptionPath[0], rlos);
	}

	public static RouteInfo calcRouteInfos(XRouteRemoteInterface client,
			Point[] points, DetailLevel detailLevel) throws SystemException,
			XServiceException, XRouteException {
		WaypointDesc[] waypoints = new WaypointDesc[points.length];
		for (int i = 0; i < waypoints.length; i++) {
			waypoints[i] = makeWayPointDesc(points[i], LinkType.NEXT_SEGMENT);
		}
		return calcRouteInfos(client, waypoints, detailLevel);
	}

	public static RouteInfo calcRouteInfos(XRouteRemoteInterface client,
			WaypointDesc[] wpts, DetailLevel detailLevel)
			throws SystemException, XServiceException, XRouteException {
		ResultListOptions rlos = new ResultListOptions();
		rlos.setDetailLevel(detailLevel);
		rlos.setPolygon(true);
		rlos.setManoeuvres(true);
		rlos.setSegments(true);
		rlos.setBinaryPathDesc(true);
		rlos.setTexts(true);
		rlos
				.setStartDetail(new DetailDescriptionOption(0,
						DetailLevel.STANDARD));
		rlos
				.setDestDetail(new DetailDescriptionOption(0,
						DetailLevel.STANDARD));
		return client.calculateRouteInfo(wpts, new RoutingOption[0],
				new ExceptionPath[0]);
	}
}
